# *-* coding:utf8 *-*
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtWidgets import QDialog
from PyQt5.QtWidgets import QTextEdit
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QVBoxLayout
import traceback
import time


class LogDialog(QDialog):
    def __init__(self):
        super(LogDialog, self).__init__()
        self.text_edit = QTextEdit()
        self.text_edit.setFontFamily('Consolas')
        self.text_edit.setFontPointSize(12)
        self.text_edit.setReadOnly(True)

        clear_button = QPushButton('清空')
        clear_button.clicked.connect(self.clear)

        v_box = QVBoxLayout()
        v_box.addWidget(self.text_edit)
        v_box.addWidget(clear_button)
        self.setLayout(v_box)
        self.setWindowTitle("日志输出")
        self.setGeometry(900, 300, 800, 700)

    def insert(self, text):
        times = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        self.text_edit.append(times + ' '+text)

    def clear(self):
        self.text_edit.clear()

    def exception_msg(self, widget, msg):
        QMessageBox.critical(widget, '异常', str(msg), QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
        self.insert(str(msg))
        self.insert('traceback.format_exc():\n%s' % traceback.format_exc())

    @staticmethod
    def error_msg(widget, msg):
        QMessageBox.critical(widget, '错误', str(msg), QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)

    @staticmethod
    def hint_msg(widget, msg):
        QMessageBox.critical(widget, '提示', str(msg), QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)

    @staticmethod
    def get_stack_info(msg):
        return 'traceback.format_exc():\n%s' % traceback.format_exc()
